<?php


namespace Modules\User\Http\Controllers\Web;


use App\Http\Controllers\MyController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Modules\User\Http\Requests\ForgetRequest;
use Modules\User\Http\Requests\RegRequest;
use Modules\User\Models\User;
use Modules\User\Http\Requests\RegNextRequest;
use Modules\User\Http\Requests\UserPwdRequest;
use Illuminate\Support\Facades\Request;


use Modules\Cms\Models\ArticleComment;
class UserController extends MyController
{
    public function index()
    {
        session([
            'the_page' => 'user',
            'page_title' => "会员中心",
        ]);
        $uid=auth()->user()->id;
      $list=ArticleComment::with('article')->where('user_id',$uid)->limit(5)->get();
     
        return $this->theme('me',['list'=>$list]);
    }
    public function reg()
    {
      $referrer = request()->get('referrer')?:request()->headers->get('referer');  
        session([
            'the_page' => 'user.reg',
            'page_title' => "会员注册",
        ]);
        $form = $this->request('from',0)?:0;
        //$form = $request->get('from',0);
        return $this->theme('reg',['form'=>$form,'referrer'=>$referrer]);
    }
    public function regnext(RegNextRequest $request)
    {
        $referrer = request()->get('referrer')?:request()->headers->get('referer');  
        $mobile = $this->request('mobile', 'mobile');
        $pwd='hao123com';
        $form = $this->request('from',0)?:0;
        session([
            'the_page' => 'user.regnext',
            'page_title' => "验证码注册",
        ]);
        return $this->theme('regnext',['mobile'=>$mobile,'pwd'=>$pwd,'form'=>$form,'referrer'=>$referrer]);
    }
      public function store(RegRequest $request, User $user)
    {
        $data = $request->validated();
        if($data['tuid']!=0 &&!User::where('id',$data['tuid'])->first()){
          $data['tuid']=0;
        }
        if (session('reg_code') == $data['reg_code'] && session('reg_mobile') == $data['mobile']) {
            unset($data['reg_code']);
            $pw=$data['password'];
            $data['password'] = Hash::make($data['password']);
            $result = $user->store($data);
            Auth::attempt(['mobile' =>$data['mobile'], 'password' => $pw, 'status' => 1]);
            return $this->result($result, ['msg' => '注册成功']);
        }
        return $this->result(false, ['msg' => "注册失败"]);
    }

    public function setpw()
    {
      $referrer = request()->get('referrer')?:request()->headers->get('referer');  
        $id=auth()->user()->id;
        session([
            'the_page' => 'user.setpw',
            'page_title' => "设置密码",
        ]);
        return $this->theme('setpw',['id'=>$id,'referrer'=>$referrer]);
    }
    public function done(UserPwdRequest $request){
        $password=$this->request('password');
        $user = User::where('id', auth()->user()->id)->first();
        if ($user) {
            $user->password = Hash::make($password);
            $result = $user->save();
            return $this->result($result, ['msg' => '修改成功']);
        }
       return $this->result(false, ['msg' => "修改失败"]);
    }
  
    public function login()
    {
        
       $referrer = request()->get('referrer')?:request()->headers->get('referer');
        session([
            'the_page' => 'user.login',
            'page_title' => "密码登陆",
        ]);

        return $this->theme('login',['referrer'=>$referrer]);
    }

    public function auth()
    {
        $name = $this->request('mobile');
        $password = $this->request('password');

        if (empty($name) || empty($password)) {
            return $this->result(false, ['msg' => "请正确填写手机号密码"]);
        }

        if (Auth::attempt(['mobile' => $name, 'password' => $password, 'status' => 1])) {
            return $this->result(true, ['msg' => "登录成功"]);
        }

        return $this->result(false, ['msg' => "验证失败，请确认账号密码后重试"]);
    }
    public function codeauth()
    {
        
        $mobile = $this->request('mobile');
        $code = $this->request('reg_code');
        if (empty($mobile) || empty($code)) {
            return $this->result(false, ['msg' => "请正确填写手机号验证码"]);
        }
        $user=User::where('mobile',$mobile)->first();
        if(!$user){
            return $this->result(false, ['msg' => "没注册或者手机号不对!"]);   
        }
        if (session('reg_code') == $code && session('reg_mobile') ==$mobile){
             if (Auth::loginUsingId($user->id, true)) {
                return $this->result(true, ['msg' => "登录成功"]);
            }
            return $this->result(false, ['msg' => "请确认"]);
        }
        return $this->result(false, ['msg' => "验证失败，请确认"]);
    }
    public function codeLogin()
    {
       $referrer = request()->get('referrer')?:request()->headers->get('referer');
        session([
            'the_page' => 'user.codeauth',
            'page_title' => "验证码登陆",
        ]);
        return $this->theme('codeogin', ['referrer'=>$referrer]);
    }
    public function loginnext()
    {
         $referrer = request()->get('referrer')?:request()->headers->get('referer');
        session([
            'the_page' => 'user.loginnext',
            'page_title' => "短信登录",
        ]);
        $mobile = $this->request('mobile', 'mobile');
        return $this->theme('loginnext',['mobile'=>$mobile, 'referrer'=>$referrer]);
    }
    public function regCode()
    {

        if (session('send_time') >= time()) {
            return $this->result(false, ['msg' => "操作频繁，请稍后再试"]);
        }

        if ($mobile = $this->request('mobile', 'mobile')) {

            $number = mt_rand(1111, 9999);
            $result = ali_sms($mobile, 0, ['code' => $number]);

            if ($result) {

                session(['reg_code' => $number]);
                session(['reg_mobile' => $mobile]);
                session(['send_time' => time() + 60]);
            }

            return $this->result($result);

        }

        return $this->result(false, ['msg' => "发送识别，请检查手机号码是否正确"]);
    }

    public function logout()
    {
        auth()->logout();

        return redirect()->intended();
    }

    public function forget()
    {
        return $this->theme('forget');
    }

    public function editPwd(ForgetRequest $request)
    {
        $data = $request->validated();

        if (session('reg_code') == $data['reg_code'] && session('reg_mobile') == $data['mobile']) {

            $user = User::where('mobile', $data['mobile'])->first();

            if ($user) {

                $user->password = Hash::make($data['password']);
                $result = $user->save();

                return $this->result($result, ['msg' => '修改成功']);
            }
        }

        return $this->result(false, ['msg' => "修改失败"]);
    }
}
